package com.rotsel.mh3udb.app.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.rotsel.mh3udb.app.Extra.Formatter;
import com.rotsel.mh3udb.app.ListModels.NumberIconTextModel;
import com.rotsel.mh3udb.app.ListModels.WeaponUpgradeMaterialModel;
import com.rotsel.mh3udb.app.ListModels.WeaponUpgradeModel;
import com.rotsel.mh3udb.app.R;
import com.rotsel.mh3udb.app.lists.MaterialList;
import com.rotsel.mh3udb.app.models.MaterialModel;
import com.rotsel.mh3udb.app.models.WeaponModel;

import java.util.ArrayList;

/**
 * Created by rots on 20.02.14.
 */
public class WeaponUpgradeAdapter extends ArrayAdapter<WeaponUpgradeModel> {
    private Context context;
    private ArrayList<WeaponUpgradeModel> modelArrayList;

    public WeaponUpgradeAdapter(Context cont, ArrayList<WeaponUpgradeModel> list){
        super(cont, R.layout.weapon_upgrade, list);

        this.context = cont;
        this.modelArrayList = list;
    }

    @Override
    public long getItemId(int position) {
        WeaponUpgradeModel weaponUpgradeModel = modelArrayList == null ? null : modelArrayList.get(position);
        return weaponUpgradeModel == null ? 0: weaponUpgradeModel.getWeaponId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.weapon_upgrade, parent, false);

        TextView priceView = (TextView)rowView.findViewById(R.id.upgradePrice);
        TextView weaponNameView = (TextView)rowView.findViewById(R.id.upgradeWeapon);

        WeaponUpgradeModel weaponUpgradeModel = modelArrayList.get(position);

        if (weaponUpgradeModel == null){
            priceView.setText("None");
            weaponNameView.setHeight(0);
            return rowView;
        }

        priceView.setText(Formatter.getPriceFormat(weaponUpgradeModel.getPrice()));
        WeaponModel weaponModel = weaponUpgradeModel.getWeapon();
        weaponNameView.setText(weaponModel.getName());

        ListView upgradeMaterialsView = (ListView)rowView.findViewById(R.id.upgradeMaterials);
        ArrayList<NumberIconTextModel> list = new ArrayList<NumberIconTextModel>();
        for(WeaponUpgradeMaterialModel item : weaponUpgradeModel.getUpgradeMaterials()){
            MaterialModel material = MaterialList.getMaterialById(item.getMaterialId());
            list.add(new NumberIconTextModel(material.getId(), item.getAmount(), material.getIcon(), material.getName()));
        }
        NumberIconTextAdapter adapter = new NumberIconTextAdapter(context, list);
        upgradeMaterialsView.setAdapter(adapter);

        return rowView;
    }
}
